define(function (require) {
    "use strict";
    var ko = require("knockout");
    var router = require("plugins/router");
    var layui = require("layui");
    var $ = require("jquery");
    var system = require("durandal/system");
    var isFinishTableTemp = require("text!./isFinish-table-temp.html");
    var isRunningTableTemp = require("text!./isRunning-table-temp.html");
    var listTableBar = require("text!./list-table-bar.html");
    var viewBar = require("text!./view-bar.html");

    var acceptPageSize = [10, 30, 60, 90, 150, 300];
    var param = {
        curr: 1,
        limit: acceptPageSize[0]
    };

    var tableModel = {
        id: "account-list",
        elem: "#list-table", //指定原始表格元素选择器（推荐id选择器）
        height: "full-227", //容器高度
        cols: [[
            //标题栏
            {field: "id", title: "ID", width: 50, fixed: "left"},
            {field: "name", title: "计划名称", width: 300},
            {field: "modifiedTime", title: "启动日期", width: 300},
            {field: "sum", title: "总任务数", width: 100},
            {field: "failed", title: "失败任务数", width: 100},
            {field: "success", title: "成功任务数", width: 100},
            {field: "running", title: "未完成任务数", width: 120},
            {title: "操作", width: 160, align: "center", toolbar: listTableBar}
        ]], //设置表头
        url: "/back/crawlResult",
        page: {
            limits: acceptPageSize,
            limit: param.limit,
            curr: param.curr
        },
        done: function (res, curr, count) {
            //设置当前页码
            param.curr = curr;
            //设置当前分页大小
            param.limit = tableIns.config.limit;
        },
        response: {
            statusCode: 200
        }
    };

    var tableIns = null;

    return {
        //router: router,
        param: param,
        /*binding: function() {
            router.activeInstruction();
            return { cacheViews: true }
        },*/
        compositionComplete: function () {
            layui.use(["laytpl", "table", "element", "layer", "laydate", "form"], function () {
                var laytpl = layui.laytpl;
                var table = layui.table;
                var element = layui.element;
                var layer = layui.layer;
                var laydate = layui.laydate;
                var form = layui.form;

                tableModel.where = {
                };
                tableModel.page.curr = param.curr;
                tableModel.page.limit = param.limit;

                tableIns = table.render(tableModel);

                table.on("tool(list-table)", function (obj) {
                    var data = obj.data;
                    if (obj.event === "download") {
                        var iframe = $("<iframe></iframe>");
                        iframe.css({ display: "none" });
                        iframe.attr("src", "/back/crawlExcel/" + data.id);
                        $("body").append(iframe);
                    }
                });

                form.on("submit(search-submit)", function (data) {
                    tableIns.reload({
                        where: {
                        },
                        page: {
                            curr: 1
                        }
                    });
                    return false;
                });

                form.on("submit(create-request)", function (data) {
                    router.navigate("plan/backAccountCreate");
                    return false;
                });
            });
        }
    };
});